﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_Timer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
            SQL sql = new SQL();
            DataSet ds = sql.getDataset("select datediff(d,ShouldReturnDate,'"+DateTime.Now.ToString()+"'),DDNUM from  Borrow_books_manage where (ShouldReturnDate < '"+DateTime.Now.ToString()+"') and returndate is  null", " ");
            DataTable dt = new DataTable();
            DataRow drow;
            DataRowCollection drc;
            dt = ds.Tables[0];
            drc = dt.Rows;
            double daymoney = Convert.ToDouble(sql.EXQSQL("select AmerceMoney from Amerce_info where AmerceTypeID='1'"));
            
            for (int i = 0; i < drc.Count; i++)
            {
                drow = drc[i];
                int day = Convert.ToInt32(drow[0].ToString());
                string ddnum=drow[1].ToString();
                string bookID = sql.EXQSQL("select bookID from Borrow_books_manage where DDNUM='" + ddnum + "'");
                string readerid = sql.EXQSQL("select readerid from Borrow_books_manage where DDNUM='" + ddnum + "'");
                double zhanghuyue = Convert.ToDouble(sql.EXQSQL("select Account from Reader_info where ReaderID='" + readerid + "'"));
                zhanghuyue=zhanghuyue-day*daymoney;
                if (sql.updateAmerce("update Amerce set Amerce='" + (day * daymoney) + "',Adate='" + DateTime.Now.ToString() + "' where AmerceID='" + ddnum + "'"))
                {
                    sql.EXQSQL("update Borrow_books_manage set Amerce='" + (day * daymoney) + "'  where DDNUM='" + ddnum + "'");
                    sql.EXQSQL("update Reader_info set Account='" + zhanghuyue + "'  where ReaderID='" + readerid + "'");
                } 
                else
                {
                    sql.EXQSQL("insert into Amerce values('"+ddnum+"','"+day * daymoney+"','1','"+DateTime.Now.ToString()+"','"+bookID+"','"+readerid+"','1','图书逾期',' ')");
                    sql.EXQSQL("update Borrow_books_manage set Amerce='" + (day * daymoney) + "'  where DDNUM='" + ddnum + "'");
                    sql.EXQSQL("update Reader_info set Account='" + zhanghuyue + "'  where ReaderID='" + readerid + "'");
                }
            }
            //在网页级别上实现刷新，无需线程刷新，否则服务器会长时间消耗资源
          //  Thread.Sleep(86400000);
        
    }
}